package movie.test.movie;

import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import movie.test.as.movie.service.MovieInfoService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;

@RestController
@RequestMapping("/movieInfo")
public class MovieInfoController {

	private static Logger logger = LoggerFactory
			.getLogger(MovieInfoController.class);
	@Autowired
	private MovieInfoService movieInfoServiceImpl;
	

	@RequestMapping("/dd")
	public void movieTest() {
		System.out.println("ttttttttttt");
	}

	/**
	 * @author lwy
	 * @deprecated:正在热映的影片和即将上映的影片前十部
	 * @param objs
	 * @param request
	 * @return
	 * @date 2017-02-27
	 */
	@ResponseBody
	@RequestMapping("/getMovieInfoList")
	public Object getMovieInfoList(@RequestBody JSONObject objs, HttpServletRequest request) {
		try {
			logger.debug("开始查询正在热映的影片信息---");
			List<Map<String, Object>> pageResult = movieInfoServiceImpl.getMovieInfoList(objs);
			logger.info("查询正在热映的影片信息结束---");
			LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<String, Object>();
			linkedHashMap.put("result", pageResult);
			return linkedHashMap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * @author lwy
	 * @deprecated:正在热映的影片和即将上映的影片
	 * @param objs
	 * @param request
	 * @return
	 * @date 2017-02-27
	 */
	@ResponseBody
	@RequestMapping("/getAllMovieInfoList")
	public Object getAllMovieInfoList(@RequestBody JSONObject objs, HttpServletRequest request) {
		try {
			logger.debug("开始查询正在热映的影片信息---");
			List<Map<String, Object>> pageResult = movieInfoServiceImpl.getAllMovieInfoList(objs);
			logger.info("查询正在热映的影片信息结束---");
			LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<String, Object>();
			linkedHashMap.put("result", pageResult);
			return linkedHashMap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 影片总数
	 */
	
	@ResponseBody
	@RequestMapping("/getCountMovieInfo")
	public Object getCountMovieInfo(@RequestBody JSONObject objs, HttpServletRequest request) {
		try {
			logger.debug("查询影片总数---");
			int count  = movieInfoServiceImpl.getCountMovieInfo(objs);
			logger.info("查询影片的总数---");
			LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<String, Object>();
			linkedHashMap.put("result", count);
			return linkedHashMap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	

	/**
	 * @author lwy
	 * @deprecated:影片信息添加
	 * @param objs
	 * @param request
	 * @return
	 * @date 2017-02-27
	 */

	/**
	 * @author lwy
	 * @deprecated:影片信息修改
	 * @param objs
	 * @param request
	 * @return
	 * @date 2017-02-27
	 */
	@ResponseBody
	@RequestMapping(value = { "/addMovieInfo" }, method = {
			RequestMethod.GET, RequestMethod.POST })
	public Object addMovieInfo(@RequestBody JSONObject objs,HttpServletRequest request){
		 try{
			 logger.debug("添加影片的信息开始---");
			int count = movieInfoServiceImpl.addMovieInfo(objs);
			 logger.info("添加影片信息结束");
			 LinkedHashMap<String,Object> resultMap = new LinkedHashMap<String,Object>();
			 if(count>0){
				 resultMap.put("successcode", count);
			 }else{
				 resultMap.put("errorcode", count);
			 }
			 return resultMap;
		 } catch(Exception e){
			 e.printStackTrace();
		 }
		return "";
		 
	 }
	 
	/**
	 * @author lwy
	 * @deprecated:影片信息删除
	 * @param objs
	 * @param request
	 * @return
	 * @date 2017-02-27
	 */
	@ResponseBody
	@RequestMapping("/deleteMovieInfoById")
	public Object deleteMovieInfoById(@RequestBody JSONObject objs,
			HttpServletRequest request) {
		try {
			logger.debug("开始删除影片信息---");
			int count = movieInfoServiceImpl.deleteMovieInfoById(objs);
			logger.info("删除的影片信息结束---");
			 LinkedHashMap<String,Object> resultMap = new LinkedHashMap<String,Object>();
			 if(count>0){
				 resultMap.put("successcode", count);
			 }else{
				 resultMap.put("errorcode", count);
			 }
			 return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * @author lwy
	 * @deprecated:影片信息查看
	 * @param objs
	 * @param request
	 * @return
	 * @date 2017-02-27
	 */
	@ResponseBody
	@RequestMapping("/getMovieInfoById")
	public Object getMovieInfoById(@RequestBody JSONObject objs,HttpServletRequest request){
		try{
			logger.debug("查看影片的信息开始----");
		    Map<String,Object> mapInfo = movieInfoServiceImpl.getMovieInfoById(objs);
			logger.info("查看影片信息结束...");
			LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<String, Object>();
			linkedHashMap.put("result", mapInfo);
			return linkedHashMap;
		} catch(Exception e){
			e.printStackTrace();
		}
		return "";
	}
	
	
	@ResponseBody
	@RequestMapping("/getSysAllMovieInfoList")
	public Object getSysAllMovieInfoList(@RequestBody JSONObject objs, HttpServletRequest request) {
		try {
			logger.debug("查询所有电影信息---");
			List<Map<String, Object>> pageResult = movieInfoServiceImpl.getSysAllMovieInfoList(objs);
			logger.info("查询所有电影信息结束---");
			LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<String, Object>();
			linkedHashMap.put("result", pageResult);
			return linkedHashMap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@ResponseBody
	@RequestMapping("/getSysfindAllHall")
	public Object getSysfindAllHall(HttpServletRequest request) {
		try {
			logger.debug("查询所有电影信息---");
			List<Map<String, Object>> pageResult = movieInfoServiceImpl.getSysfindAllHall();
			logger.info("查询所有电影信息结束---");
			LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<String, Object>();
			linkedHashMap.put("result", pageResult);
			return linkedHashMap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	@ResponseBody
	@RequestMapping("/findAllCategory")
	public Object findAllCategory(HttpServletRequest request) {
		try {
			logger.debug("查询所有电影信息---");
			List<Map<String, Object>> pageResult = movieInfoServiceImpl.findAllCategory();
			logger.info("查询所有电影信息结束---");
			LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<String, Object>();
			linkedHashMap.put("result", pageResult);
			return linkedHashMap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@ResponseBody
	@RequestMapping(value = { "/updateMovieInfo" }, method = {
			RequestMethod.GET, RequestMethod.POST })
	public Object updateMovieInfo(@RequestBody JSONObject objs,HttpServletRequest request){
		 try{
			 logger.debug("修改影片的信息开始---");
			int count = movieInfoServiceImpl.updateMovieInfo(objs);
			 logger.info("修改影片信息结束");
			 LinkedHashMap<String,Object> resultMap = new LinkedHashMap<String,Object>();
			 if(count>0){
				 resultMap.put("successcode", count);
			 }else{
				 resultMap.put("errorcode", count);
			 }
			 return resultMap;
		 } catch(Exception e){
			 e.printStackTrace();
		 }
		return "";
		 
	 }
	/**
	 * 上传影片信息
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@ResponseBody
	@RequestMapping(value={"/uploadMovieById"},method={RequestMethod.GET,RequestMethod.POST})
	public Object uploadMovieById(@RequestParam MultipartFile file,HttpServletRequest request) throws IllegalStateException, IOException{
	try{	  
		String id = request.getParameter("id");  
		  LinkedHashMap<String,Object> result = new LinkedHashMap<String,Object>();
		if(!file.isEmpty()){
			 String fileName = file.getOriginalFilename();
			 String filePathName = "upload/"+fileName;
			 //System.out.println(fileName);//ww.jpg
			//String path = request.getSession().getServletContext().getRealPath("upload");
		   File targetFile = new File("E:\\upload",fileName);
		   if(!targetFile.exists()){
			   targetFile.mkdirs();
		   }
				file.transferTo(targetFile);
				JSONObject objs = new JSONObject();
				objs.put("id", id);
				objs.put("filePathName", filePathName);
				int count = movieInfoServiceImpl.updateUploadMovie(objs);
				 result.put("successcode",count); 
		}
		
		return result;
	 } catch(Exception e){
		 e.printStackTrace();
	 }
	return "";
			    	
			    
	}
	/**
	 * 上传影片信息
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@ResponseBody
	@RequestMapping(value={"/uploadPersonById"},method={RequestMethod.GET,RequestMethod.POST})
	public Object uploadPersonById(@RequestParam MultipartFile[] file,HttpServletRequest request) throws IllegalStateException, IOException{
		// String[] fileName = file.getOriginalFilename();
	return "";
			    	
			    
	}
	
}
